﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Wormhole.Engine.Communication.Messages
{
	/// <author>Hannes Foulds</author>
	/// <date>17 September 2008</date>
	/// <summary>
	/// This class handles messages related to joining games.
	/// </summary>
	public class JoinGameMessage : Message
	{
		#region Properties

		#region Response
		/// <summary>
		/// The response string message.
		/// </summary>
		public string Response { get; set; }
		#endregion

		#region Game List
		/// <summary>
		/// A list of open games.
		/// </summary>
		public Dictionary<Guid, Game> GameList { get; set; }
		#endregion

		#endregion

		#region Constructor
		/// <summary>
		/// Initialize a new instance of the class.
		/// </summary>
		/// <param name="game">The game the message relates to.</param>
		/// <param name="action">The message action.</param>
		public JoinGameMessage(Game game, Actions action)
		{
			this.Game = game;
			this.Action = action;
		}

		/// <summary>
		/// Initialize a new instance of the class.
		/// </summary>
		/// <param name="game">The game the message relates to.</param>
		/// <param name="action">The message action.</param>
		/// <param name="response">The response string message.</param>
		public JoinGameMessage(Game game, Actions action, string response) : this(game, action)
		{
			this.Response = response;
		}

		/// <summary>
		/// Initialize the class with the values provided.
		/// </summary>
		/// <param name="message">The string message to parse.</param>
		public JoinGameMessage(string message, Actions action, Dictionary<Guid, Game> gameList)
		{
			this.Action = action;
			this.GameList = gameList;
			this.Parse(message);
		}
		#endregion

		#region To String
		/// <summary>
		/// Convert the message to a string that can be send via the communications channel.
		/// </summary>
		/// <returns>Returns the string representation of the message.</returns>
		public override string ToString()
		{
			if (this.Action == Actions.RequestJoin)
				return string.Format("{0}|{1}", EnumConvertor<Actions>.ToString(this.Action), this.Game.ID);
			else if ((this.Action == Actions.AcceptJoin) || (this.Action == Actions.RejectJoin))
				return string.Format("{0}|{1}|{2}", EnumConvertor<Actions>.ToString(this.Action), this.Game.ID, this.EncodeString(this.Response));
	
			return null;
		}
		#endregion

		#region Parse
		/// <summary>
		/// Parse a received game list message.
		/// </summary>
		/// <param name="message">The string message to parse.</param>
		private void Parse(string message)
		{
			string[] messageParts = message.Split('|');
			Guid gameID = new Guid(messageParts[1]);

			// get the response message
			if ((this.Action == Actions.AcceptJoin) || (this.Action == Actions.RejectJoin))
			{
				this.Response = this.DecodeString(messageParts[2]);
				this.Game = this.GameList[gameID];
			}
			else
			{
				this.Game = new Game();
				this.Game.ID = gameID;
			}
		}
		#endregion
	}
}
